//app.js
var md5 = require('./libs/md5.js');
App({
  static: {
    url: 'https://m.guaiguaixiu.com/'
  },
  onLaunch: function () {
    this.getopenid();
  },
 
  /***获取openid***/
  getopenid: function () {
    var that = this;
    wx.login({
      success(res) {
        if (res.code) {
          var login = {
            url: 'api/userr/wexinLogin',
            data: {
              code: res.code
            },
            success: function (e) {
              console.log(e)
              var userid = e.data.data.user_id;
              var status = e.data.data.status;
              console.log('appstatus',status);
              var shop_id = e.data.data.shop_id;
              var role = e.data.data.role;
              var shop_type = e.data.data.shop_type;
              var shop_status = e.data.data.shop_status;
              wx.setStorageSync('userId', userid);
              wx.setStorageSync('status', status);
              wx.setStorageSync('shop_id', shop_id);
              wx.setStorageSync('role', role);
              wx.setStorageSync('shop_type', shop_type);
              wx.setStorageSync('shop_status', shop_status);
              // if (parseInt(role) == 0) {
                
              // }
              // if (parseInt(shop_type) == 2 && parseInt(role) == 1) {
                
              //   wx.redirectTo({
              //     url: '/pages/shangjia/shangjia/shangjia',
              //   })
              // }

              // if (parseInt(shop_type) == 2 && parseInt(role) == 2) {

              //   wx.redirectTo({
              //     url: '/pages/shangjia/shangjia/shangjia',
              //   })
              // }

              // if (parseInt(shop_type) == 1 || parseInt(shop_type) == 3) {
              
              //   wx.redirectTo({
              //     url: '/pages/shangjia/zichan/zichan',
              //   })
              // }
            }
          };
          that.post(login);
        } else {
          console.log('登录失败！' + res.errMsg)
        }
      }
    })
  },

  post:function(obj){
    var timestamp = Date.parse(new Date()) / 1000;
    var random = Math.floor((Math.random() * 100)+1);
    var sign = 'GHRDY65' + String(random) + String(timestamp);
    let params = obj.data ? obj.data : {}
    params.timestamp = timestamp;
    params.random = random;
    params.sign = md5.hexMD5(sign);
    wx.request({
      url: this.static.url + obj.url,
      data: params,
      method: 'POST',
      success: function (res) {
        obj.success ? obj.success(res) : ""
      },
      fail:function(res){
        obj.fail ? obj.fail(res) : '';  
      }
    })
  },
  uploadimg:function(success){
    var timestamp = Date.parse(new Date()) / 1000;
    var random = Math.floor(Math.random() * 100);
    var sign = 'GHRDY65' + String(random) + String(timestamp);
    sign = md5.hexMD5(sign);
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
          wx.uploadFile({
            url: that.static.url + 'api/common/uploadFile',
            filePath: res.tempFilePaths[0],
            name: 'file',
            formData: {
              timestamp:timestamp,
              random:random,
              sign:sign
            },
            success: function (ret) {
              success(ret);
            }
          })
        }
    })
  },
 
  globalData: {
    userInfo: null,
    detail:''
  }
})